package cn.wen.base.adapter.recyclerview;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * RecyclerView的通用的适配器
 */

public abstract class QuickRecyclerAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    protected List<T> mData;
    protected Context mContext;
    protected int mLayoutId;
    private QuickMultiSupport<T> mSupport;
    private LayoutInflater mInflater;

    public QuickRecyclerAdapter(Context context, List<T> data, int layoutId) {
        this.mData = data == null ? new ArrayList<T>() : new ArrayList<>(data);
        this.mContext = context;
        this.mLayoutId = layoutId;
        mInflater = LayoutInflater.from(mContext);
    }

    public QuickRecyclerAdapter(Context context, List<T> data, QuickMultiSupport<T> support) {
        this(context, data, 0);
        this.mSupport = support;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 如果是多条目，viewType就是布局ID
        View view;
        if (mSupport != null) {
            view = mInflater.inflate(viewType, parent, false);
        } else {
            view = mInflater.inflate(mLayoutId, parent, false);
        }

        QuickRecyclerViewHolder holder = new QuickRecyclerViewHolder(view);
        return holder;
    }


    @Override
    public int getItemViewType(int position) {
        if (mSupport != null) {
            // 返回的就是布局ID
            return mSupport.getLayoutId(mData.get(position));
        }
        return super.getItemViewType(position);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof QuickRecyclerViewHolder) {
            convert((QuickRecyclerViewHolder) holder, mData.get(position), position);
        }

    }

    /**
     * 绑定View的数据
     */
    protected abstract void convert(QuickRecyclerViewHolder holder, T item, int position);

    @Override
    public int getItemCount() {
        return mData.size();
    }


    //==========================================数据相关================================================
    public void add(T elem) {
        mData.add(elem);
        notifyDataSetChanged();
    }

    public void addAll(List<T> data) {
        mData.addAll(data);
        notifyDataSetChanged();
    }

    public void addFirst(T elem) {
        mData.add(0, elem);
        notifyDataSetChanged();
    }

    public void set(T oldElem, T newElem) {
        set(mData.indexOf(oldElem), newElem);
    }

    public void set(int index, T elem) {
        mData.set(index, elem);
        notifyDataSetChanged();
    }

    public void remove(T elem) {
        mData.remove(elem);
        notifyDataSetChanged();
    }

    public void remove(int index) {
        mData.remove(index);
        notifyItemRemoved(index);
    }

    public void replaceAll(List<T> elem) {
        mData.clear();
        mData.addAll(elem);
        notifyDataSetChanged();
    }

    public boolean contains(T elem) {
        return mData.contains(elem);
    }

    /**
     * 清除
     */
    public void clear() {
        mData.clear();
        notifyDataSetChanged();
    }

    public List<T> getData() {
        return mData;
    }


    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        if (mSupport == null) {
            return;
        }
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
            final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();
            // 如果是头部和底部就占用SpanCount那个多个位置
            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int spanSize = mSupport.getSpanSize(mData.get(position));
                    if (mSupport.isSpan(mData.get(position))) {
                        return gridLayoutManager.getSpanCount();
                    } else if (spanSize > 0) {
                        return spanSize;
                    } else if (spanSizeLookup != null) {
                        return spanSizeLookup.getSpanSize(position);
                    }
                    return 1;
                }
            });
            gridLayoutManager.setSpanCount(gridLayoutManager.getSpanCount());
        }
    }

    @Override
    public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
        if (mSupport == null) {
            return;
        }
        try {
            int position = holder.getLayoutPosition();
            // 如果是头部和底部就合并单元格
            T t = mData.get(position);
            if (mSupport.isSpan(t)) {
                ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
                if (lp != null && lp instanceof StaggeredGridLayoutManager.LayoutParams) {
                    StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp;
                    p.setFullSpan(true);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
